package com.crk.yiyuanguahao.controller;

import com.crk.yiyuanguahao.entity.Function;
import com.crk.yiyuanguahao.entity.ResAuth;
import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.entity.menu.SysFunctionMenuDto;
import com.crk.yiyuanguahao.entity.menu.SysFunctionTreeDto;
import com.crk.yiyuanguahao.service.ResAuthService;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * Description:
 *
 * @author:
 * Date: 2020-01-08
 * Time: 15:24
 */
@RestController
@RequestMapping("resAuth")
public class ResAuthController {
    @Autowired
    ResAuthService resAuthService;

    private static Logger logger = Logger.getLogger(ResAuthController.class);
    /**
     * 添加功能权限
     * @param resAuth 功能权限
     * @return
     */
    @RequestMapping("/addResAuth")
    public ResponseResult addResAuth(ResAuth resAuth){
        return resAuthService.addResAuth(resAuth);
    }

    /**
     * 批量添加功能权限
     * @param data 数据
     * @return
     */
    @RequestMapping("/batchAddResAuth")
    public ResponseResult batchAddResAuth(@RequestBody HashMap<String,Object> data){
        int roleId = (Integer) data.get("roleId");
        List<HashMap<String,Integer>> res = (List<HashMap<String,Integer>>)data.get("res");
        return resAuthService.batchAddResAuth(roleId,res);
    }

    /**
     * 修改功能权限
     * @param resAuth 功能权限
     * @return
     */
    @RequestMapping("/updateResAuth")
    public ResponseResult updateResAuth(ResAuth resAuth){
        return resAuthService.updateResAuth(resAuth);
    }

    /**
     * 批量修改功能权限
     * @param data 数据
     * @return
     */
    @RequestMapping("/batchUpdateResAuth")
    public ResponseResult batchUpdateResAuth(@RequestBody HashMap<String,Object> data){
        int roleId = Integer.parseInt(data.get("roleId").toString());
        List<HashMap<String,Integer>> res = (List<HashMap<String,Integer>>)data.get("res");
        return resAuthService.batchUpdateResAuth(roleId,res);
    }

    /**
     * 删除功能权限
     * @param resAuthId 权限id
     * @return
     */
    @RequestMapping("/deleteResAuth")
    public ResponseResult deleteResAuth(@RequestParam int resAuthId){
        return resAuthService.deleteResAuth(resAuthId);
    }

    /**
     * 查询功能权限列表
     * @return
     */
    @RequestMapping("/queryResAuthList")
    public ResponseResult queryResAuthList(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return resAuthService.queryResAuthList(user.getId());
    }

    /**
     * 根据角色查询功能权限(菜单)
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/queryResListTreeByRole")
    public ResponseResult queryResListTreeByRole(@RequestParam int roleId, int functionId, int type){
        return resAuthService.queryResListTreeByRole(roleId,functionId,type);
    }

    /**
     * 根据功能id和子功能类型查询下级功能列表
     * @param functionId
     * @param type
     * @return
     */
    @RequestMapping("/queryResChildren")
    public ResponseResult queryResChildren(int functionId,int type){
        List<SysFunctionTreeDto> list = new ArrayList<SysFunctionTreeDto>();
        list = resAuthService.getChildrenTree(list,null,functionId,type);
        return ResponseResult.success(list);
    }

    /**
     * 根据父id查询子功能列表
     * @param pId
     * @param type
     * @param queryType 查询类型
     * @return
     */
    @RequestMapping("/queryMenuListByPid")
    public ResponseResult queryMenuListByPid(int pId,@RequestParam(defaultValue = "0") int type
            ,@RequestParam(defaultValue = "0") int queryType){
        try {
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            if (queryType == 1){
                List<Function> list = resAuthService.selectByParentIdPlus(pId,type,user.getId());
                return ResponseResult.success(list);
            }else {
                List<Function> list = resAuthService.selectByParentId(pId,type);
                return ResponseResult.success(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail();
        }

    }
    /**
     * 根据父id查询子功能列表
     * @param pId
     * @param type
     * @return
     */
    @RequestMapping("/queryMenuListAndChildrenByPid")
    public ResponseResult queryMenuListAndChildrenByPid(int pId,@RequestParam(defaultValue = "0") int type){
        List<SysFunctionMenuDto> list = new ArrayList<SysFunctionMenuDto>();
        try {
            ResponseResult roleAuthList = queryResAuthList();
            if (null == roleAuthList || !ResponseResult.isSuccess(roleAuthList) || roleAuthList.getData() == null){
                return null;
            }
            List<Function> functions = (List<Function>)roleAuthList.getData();
            HashSet<Integer> keyMap = new HashSet<Integer>();
            for (int i=0;i<functions.size();i++){
                keyMap.add(functions.get(i).getId());
            }
            list = resAuthService.queryMenuListByPid(list,keyMap,pId,type);
            return ResponseResult.success(list);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail();
        }

    }

    @RequestMapping("/getFunctionByUser")
    public ResponseResult getFunctionByUser(@RequestParam(defaultValue = "1") int type, int currentId){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return resAuthService.getFunctionByUser(user.getId(),type,currentId);
    }

    @RequestMapping("/getFunctionTreeByUser")
    public ResponseResult getFunctionTreeByUser(@RequestParam(defaultValue = "1") int type, int currentId){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return resAuthService.getFunctionTreeByUser(user.getId(),type,currentId);
    }

    @RequestMapping("/getFunctionTreeByRole")
    public ResponseResult getFunctionTreeByRole(@RequestParam int roleId, @RequestParam(defaultValue = "1") int type, Integer currentId){
        return resAuthService.getFunctionTreeByRole(roleId,type,currentId);
    }
}
